package com.wry.file.utils;

import com.wry.file.controller.ExcelController;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;

/**
 * <h1>  <h1>
 *
 * @author wry
 * @since 2024/9/14 10:55
 */
public class PoiTemplateExportUtil {

    public static void write(Sheet sheet, int dataStartRowIndex, List<ExcelController.Goods.Info> infoList) {
        // 初始化 code 值
        String previousCode = infoList.get(0).getCode();
        int mergeStartRow = 3; // 合并起始行，第四行为索引 3

        // 获取第四行的样式作为模板
        Row fourthRow = sheet.getRow(3); // 第四行是第三个索引
        CellStyle headerStyle = fourthRow.getCell(0).getCellStyle();
        // 字段数量
        int fieldNumber = ExcelController.Goods.Info.class.getDeclaredFields().length;

        // 从第四行开始插入新数据
        for (int i = 0; i < infoList.size(); i++) {
            Row row = sheet.createRow(dataStartRowIndex + i);
            // 复制样式到新行的每个单元格
            for (int j = 0; j < fieldNumber; j++) {
                Cell cell = row.createCell(j);
                cell.setCellStyle(headerStyle);
            }

            // 赋值
            ExcelController.Goods.Info info = infoList.get(i);
            row.getCell(0).setCellValue(info.getNo());
            row.getCell(1).setCellValue(info.getCode());
            row.getCell(2).setCellValue(info.getName());
            row.getCell(3).setCellValue(info.getCnName());
            row.getCell(4).setCellValue(info.getEnName());
            row.getCell(5).setCellValue(info.getHl());
            row.getCell(6).setCellValue(info.getBfb());
            row.getCell(7).setCellValue(info.getCfhl());
            row.getCell(8).setCellValue(info.getMd());
            row.getCell(9).setCellValue(info.getXb());
            row.getCell(10).setCellValue(info.getXh());
            row.getCell(11).setCellValue(info.getScs());
            row.getCell(12).setCellValue(info.getBsm());
            row.getCell(13).setCellValue(info.getRemark());

            // 垂直合并逻辑：判断code是否相同
            if (previousCode.equals(info.getCode())) {
                continue; // 如果相同，则继续下一个循环
            } else {
                // 垂直合并no, code, xb列
                if (mergeStartRow != dataStartRowIndex + i - 1) {
                    sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + i - 1, 0, 0)); // no列
                    sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + i - 1, 1, 1)); // code列
                    sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + i - 1, 9, 9)); // xb列
                }
                mergeStartRow = dataStartRowIndex + i; // 更新merge的起始行
            }

            previousCode = info.getCode(); // 更新previousCode
        }
        // 合并最后一组相同code的数据
        if (mergeStartRow != dataStartRowIndex + infoList.size() - 1) {
            sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + infoList.size() - 1, 0, 0)); // no列
            sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + infoList.size() - 1, 1, 1)); // code列
            sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + infoList.size() - 1, 9, 9)); // xb列
        }
    }

//    // 产品配方（含原料版）表
//    final Sheet sheet = workbook.getSheet("配方表 (3)");
//    // 产品名称
//    Row secondRow = sheet.getRow(1);
//    Cell productNameCell = secondRow.getCell(0);
//            productNameCell.setCellValue(filingStorage.getProductName());
//
//    // 第四行开始
//    final int dataSize = sortedProductFormulaIncludingMaterialExcelDataList.size();
//    // 初始itemNumber
//    int previousItemNumber = sortedProductFormulaIncludingMaterialExcelDataList.get(0).getItemNumber();
//    // 数据起始行
//    int dataStartRowIndex = 3;
//    // 合并起始行
//    int mergeStartRow = 3;
//
//    // 样式
//    CellStyle rowStyle = sheet.getRow(dataStartRowIndex).getCell(0).getCellStyle();
//    // 字段数量
//    int numberOfFields =ProductFormulaIncludingMaterialExcelDataVo.class.getDeclaredFields().length;

//        sheet.shiftRows(dataStartRowIndex + 1, sheet.getLastRowNum(), dataSize - 1, true, false);
//
//            for (int index = 0; index < dataSize; index++) {
//        // 创建行 单元格
//        Row row = sheet.createRow(dataStartRowIndex + index);
//        for (int idx = 0; idx < numberOfFields; idx++) {
//            Cell cell = row.createCell(idx);
//            cell.setCellStyle(rowStyle);
//        }
//
//        final ProductFormulaIncludingMaterialExcelDataVo currentData = sortedProductFormulaIncludingMaterialExcelDataList.get(index);
//        row.getCell(0).setCellValue(currentData.getItemNumber());
//        row.getCell(1).setCellValue(currentData.getRawMaterialCode());
//        row.getCell(2).setCellValue(currentData.getRawMaterialGoodsName());
//        row.getCell(3).setCellValue(currentData.getIngredientCnName());
//        row.getCell(4).setCellValue(currentData.getIngredientEnName());
//        row.getCell(5).setCellValue(currentData.getRawMaterialContent().toString());
//        row.getCell(6).setCellValue(currentData.getRawMaterialRate().toString());
//        row.getCell(7).setCellValue(currentData.getActualIngredientContent().toString());
//        row.getCell(8).setCellValue(currentData.getRawMaterialIntendedUse());
//        row.getCell(9).setCellValue(currentData.getRawMaterialDifferentiation());
//        row.getCell(10).setCellValue(currentData.getDirectoryNumber());
//        row.getCell(11).setCellValue(currentData.getProducer());
//        row.getCell(12).setCellValue(currentData.getSubmitCode());
//        row.getCell(13).setCellValue(currentData.getRemark());
//
//        if (previousItemNumber == currentData.getItemNumber()) {
//            continue;
//        } else {
//            // 合并
//            if (mergeStartRow != dataStartRowIndex + index - 1) {
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 0, 0));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 1, 1));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 2, 2));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 5, 5));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 8, 8));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 9, 9));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 11, 11));
//                sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + index - 1, 12, 12));
//            }
//            // 合并起始行后移为当前行
//            mergeStartRow = dataStartRowIndex + index;
//        }
//        // 更新为当前行项次
//        previousItemNumber = currentData.getItemNumber();
//    }
//
//    // 合并最后一组
//            if (mergeStartRow != dataStartRowIndex + dataSize - 1) {
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 0, 0));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 1, 1));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 2, 2));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 5, 5));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 8, 8));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 9, 9));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 11, 11));
//        sheet.addMergedRegion(new CellRangeAddress(mergeStartRow, dataStartRowIndex + dataSize - 1, 12, 12));
//    }
}
